<?php
/**
 * Created by PhpStorm.
 * User: woddp
 * Date: 2017/3/5
 * Time: 16:26
 */

namespace Admin\Controller;

class MoveController extends CommonController{

    public function index() {
        $this->display('list');
    }

    public function add() {
        $this->display();
    }

    public function edit() {
        $this->display();
    }

    /**
     * 获取一条数据
     */

    public function get() {
        $id = I('get.id');
        if(!isset($id) || empty($id)) {
            $this->ajaxReturn(['status' => false, 'msg' => '参数错误']);
        }
        $moveModel = M('move');
        $data      = $moveModel->where(['id' => $id])->find();

        $data['moveintro'] = htmlspecialchars_decode($data['moveintro']);
        $image             = new ImagesController(); //获取图片
        //获取logo
        $logoId       = $data['logo'];
        if(!empty($logoId)) {
            $imgUrl       = $image->getImg($logoId);
            $data['logo'] = ['url' => $imgUrl[0]['path'], 'id' => $logoId];
        }
        //获取图片集
        $imgsId       = $data['imgs'];
        if(!empty($imgsId)) {
            $imgUrls      = $image->getImg($imgsId);
            $data['imgs'] = [];
            foreach($imgUrls as $v) {
                array_push($data['imgs'], ['url' => $v['path'], 'id' => $v['id']]);
            }
        }
        $data['createtime']=date('Y/m/d h:i:s',$data['createtime']);
        $data['updatetime']=date('Y/m/d h:i:s',$data['updatetime']);
        if(!empty($data)) {
            $this->ajaxReturn(['status' => true, 'msg' => $data]);
        } else {
            $this->ajaxReturn(['status' => false, 'msg' => '数据获取失败']);
        }
    }

    /**
     * 获取所有数据
     */
    public function gets() {
        $pages = I('get.');
        //每页显示
        $pageView = empty($pages['pageView']) ? 10 : $pages['pageView'];
        //当前页
        $pageCurr  = empty($pages['pageCurr']) ? 1 : $pages['pageCurr'];
        $category_id=$pages['categoryId'];
        $pageCurr  = ($pageCurr - 1) * $pageView;
        $moveModel = M('move');
        $condition=[];//条件
        if($category_id!=1){
            $condition['move.category_id']=$category_id;

        }
        $ret       = $moveModel->join('category on move.category_id=category.id')->where($condition)->field('category.name as category_name, move.id,move.title,move.intro,move.logo,move.status,move.createtime')->order('id desc')->limit($pageCurr, $pageView)->select();
        if(empty($ret)){
            $this->ajaxReturn(['status' => false, 'msg' => '数据不存在']);
        }
        array_walk($ret, function (&$v) {
            $v['createtime'] = date('Y/m/d h:i:s');
        });
        $count = $moveModel->count();
        $this->ajaxReturn(['status' => true, 'msg' => ['count' => $count, 'data' => $ret]]);
    }

    public function del() {
        $id = I('get.id');
        $id = intval($id);
        if(!isset($id) || empty($id)) {
            $this->ajaxReturn(['status' => false, 'msg' => '参数错误']);
        }
        $moveModel = M('move');
        $status    = $moveModel->where(['id' => $id])->delete();
        if($status) {
            $this->ajaxReturn(['status' => true, 'msg' => '删除成功']);
        } else {
            $this->ajaxReturn(['status' => false, 'msg' => '删除失败']);
        }
    }

    public function count() {
        $moveModel = M('move');
        $count     = $moveModel->count();
        $this->ajaxReturn(['status' => true, 'msg' => $count]);
    }

    public function save() {
        if(IS_POST) {
            $data = I('post.');
            if(empty($data['title'])) {
                return $this->ajaxReturn(['status' => false, 'msg' => '标题不能为空']);
            }
            if(empty($data['moveintro'])) {
                return $this->ajaxReturn(['status' => false, 'msg' => '简介不能为空']);
            }
            if(empty($data['resource'])) {
                return $this->ajaxReturn(['status' => false, 'msg' => '资源不能为空']);
            }
            $data['title']      = trim($data['title']);
            $data['intro']      = empty($data['intro']) ? substr(strip_tags(html_entity_decode($data['moveintro'])), 0, 550) : $data['intro'];
            $data['createtime'] = $data['updatetime'] = time();
            $moveModel          = M('move');
            $status             = $moveModel->add($data);
            if($status) {
                return $this->ajaxReturn(['status' => true, 'msg' => '添加成功']);
            } else {
                return $this->ajaxReturn(['status' => false, 'msg' => '添加失败，请重试！']);
            }
        } else {
            echo '非法访问';
        }
    }

    public function update() {
        if(IS_POST) {
            $data = I('post.');
            if(empty($data['title'])) {
                return $this->ajaxReturn(['status' => false, 'msg' => '标题不能为空']);
            }
            if(empty($data['moveintro'])) {
                return $this->ajaxReturn(['status' => false, 'msg' => '简介不能为空']);
            }
            if(empty($data['resource'])) {
                return $this->ajaxReturn(['status' => false, 'msg' => '资源不能为空']);
            }
            $data['title']      = trim($data['title']);
            $data['intro']      = empty($data['intro']) ? substr(strip_tags(html_entity_decode($data['moveintro'])), 0, 550) : $data['intro'];
             $data['updatetime'] = time();
            $moveModel          = M('move');
            $status             = $moveModel->save($data);
            if($status) {
                return $this->ajaxReturn(['status' => true, 'msg' => '修改成功']);
            } else {
                return $this->ajaxReturn(['status' => false, 'msg' => '修改失败，请重试！']);
            }
        } else {
            echo '非法访问';
        }
    }
}